package com.njsoft.mydemotest;

import android.app.DialogFragment;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.njsoft.mydemotest.pic.tools.RGlideUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 1、数据的封装（本地数据，或者网络数据），点击购买后数据的处理！
 * 2、列表显示的优化(根据数据)
 * 3、点击确认时数据的回掉
 * 4、电梯图片时显示大图
 */
public class GoodAttributeFragmentDialog extends DialogFragment {
    private String[] mVals = new String[]
            {"Hello", "Android", "Weclome Hi ", "Button", "TextView", "Hello",
                    "Android", "Weclome", "Button ImageView", "TextView", "Helloworld",
                    "Android", "Weclome Hello", "Button Text", "TextView"};
    private RecyclerView GoodList;
    private Button GoodCommit;
    private GoodAttributeAdapter attributeAdapter;
    private List<GoodAttributeBean> beanList = new ArrayList<>();
    private ImageView GoodImgShow;

    public static GoodAttributeFragmentDialog newInstance() {
        Bundle args = new Bundle();
        GoodAttributeFragmentDialog fragment = new GoodAttributeFragmentDialog();
        fragment.setArguments(args);
        return fragment;
    }

    private void handler() {

        //显示图片
        RGlideUtil.setImage(getActivity(), "https://img5.duitang.com/uploads/item/201404/11/20140411214939_XswXa.jpeg", GoodImgShow);
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.BOTTOM;
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(params);
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(R.layout.good_attribute_select_dialog, container, false);
        AnimationUtils.slideToUp(view);
        GoodList = view.findViewById(R.id.GoodAttributeList);
        GoodCommit = view.findViewById(R.id.GoodConfirm);
        GoodCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<GoodHadSelectBean> beanList = attributeAdapter.getHadSelectGoodAttributList();
                String ShowGoodMsg = "请选择 ";
                for (int i = 0; i < beanList.size(); i++) {
                    if(TextUtils.isEmpty(beanList.get(i).getGoodAttribute())){
                        ShowGoodMsg+=beanList.get(i).getGoodClassAttributeName()+" ";
                    }
                }
                if (ShowGoodMsg.length()==4) {
                    //TODO 处理添加购物车，或者直接购买逻辑
                    dismiss();
                }else {
                    Toast.makeText(getActivity(), ShowGoodMsg, Toast.LENGTH_SHORT).show();
                }
            }
        });
        GoodImgShow = view.findViewById(R.id.img);
        handler();
        GoodImgShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //跳转和传数据都必须要
                Intent intent = new Intent(getActivity(), PicShowActivity.class);
                startActivity(intent);
                getActivity().overridePendingTransition(R.anim.activity_in, 0);

            }
        });
        attributeAdapter = new GoodAttributeAdapter(getActivity(), goodAttributeBeanList());
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        GoodList.setLayoutManager(layoutManager);
        GoodList.setAdapter(attributeAdapter);
        //TODO 数据封装逻辑


        return view;
    }

    private List<GoodAttributeBean> goodAttributeBeanList() {
        for (int count = 0; count < 3; count++) {
            GoodAttributeBean bean = new GoodAttributeBean();
            bean.setGoodAttributeName("测试" + count);
            List<String> stringList = new ArrayList<>();
            for (int i = 0; i < mVals.length; i++) {
                stringList.add(mVals[i] + ":" + count);
            }
            bean.setGoodAttributList(stringList);
            beanList.add(bean);
        }
        return beanList;
    }


}

